<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/13
 * Time: 15:51
 */

if (!defined('IN_IA')) {
    exit('Access Denied');
}

class Cate_MxShopPage extends WebPage {

    public function main() {
        global $_W, $_GPC;

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        $keyword = $_GPC['keyword'];
        $condition = '';
        if (!empty($keyword)) {
            $condition .= " AND name LIKE '%{$keyword}%' ";
        }
        $status = intval($_GPC['status']);
        if (!empty($status)) {
            if ($status == 2) {
                $status2 = 0;
            } else {
                $status2 = $status;
            }
            $condition .= ' AND status=' . $status2;
        }

        $list = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_mendian_cate') . " WHERE 1 " . $condition . " AND deleted=0 ORDER BY displayorder DESC,id ASC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
        $total = pdo_fetchcolumn("SELECT count(id) FROM " . tablename('mx_shop_mendian_cate') . "  WHERE 1 AND deleted=0" . $condition);

        $pager = pagination($total, $pindex, $psize);

        include $this->template();
    }

    public function post(){

        global $_W, $_GPC;
        $uniacid = $_W['platformid'];

        $id = intval($_GPC ['id']);

        if (!empty ($id)) {
            $item = pdo_fetch("SELECT * FROM " . tablename('mx_shop_mendian_cate') . " WHERE id = '$id'");
        } else {
            $item = array(
                'displayorder' => 0,
                'thumb' => '',
                'status' => 1
            );
        }

        if ($_W['ispost']) {

            if (empty ($_GPC ['catename'])) {
                show_json(0, '抱歉，请输入门店分类名称！');
            }

            $mcname = pdo_fetch("SELECT id FROM " . tablename('mx_shop_mendian_cate') . " WHERE id != :id AND name = :mcname AND deleted=0", array(':id' => $id, ':mcname' => $_GPC['catename']));
            if(!(empty($mcname['id']))){
                show_json(0,"该门店分类名已存在");
            }

            $temp = pdo_fetch("SELECT id FROM " . tablename('mx_shop_mendian_cate') . " WHERE 1 AND name='" . $_GPC ['catename'] . "'");
            if (!empty ($temp) && $id != $temp ['id']) {
                show_json(0, '已存在该门店分类名称！');
            }
            $data = array(
                'uniacid' => $uniacid,
                'name' => $_GPC ['catename'],
                'description' => $_GPC ['description'],
                'status' => intval($_GPC ['status']),
                'thumb' => $_GPC ['thumb'],
                'displayorder' => intval($_GPC ['displayorder']),
                'updatetime' => TIMESTAMP
            );
            if (!empty ($id)) {
                pdo_update('mx_shop_mendian_cate', $data, array('id' => $id));
                plog('mendian.cate.add', '更新"' . $data['name'] . '"门店分类成功！门店分类ID为' . $id);
            } else {
                $data['createtime'] = $data['updatetime'];
                pdo_insert('mx_shop_mendian_cate', $data);
                $id = pdo_insertid();
                plog('mendian.cate.add', '新建"' . $data['name'] . '"门店分类成功！门店分类ID为' . $id);
            }
            show_json(1, array('url'=>webUrl('mendian/cate')));

        }

        include $this->template();
    }

    public function status(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        $status = intval($_GPC['status']);

        if(!empty($id)){
            if($status == 1){
                pdo_update('mx_shop_mendian_cate', array( 'status' => 1), array('id' => $id));
            }else{
                pdo_update('mx_shop_mendian_cate', array( 'status' => 0), array('id' => $id));
            }
        }
        show_json(1, array('url'=>webUrl('mendian/cate')));

    }

    public function delete(){

        global $_W, $_GPC;

        $id = intval($_GPC['id']);
        if (empty($id)) {
            $id = is_array($_GPC['ids']) ? implode(',', $_GPC['ids']) : 0;
        }

        $md = pdo_fetchall("SELECT id FROM " . tablename('mx_shop_mendian') . " WHERE mdcate_id in( $id ) AND deleted=0 ");
        if(!empty($md)){
            show_json(0, '要删除该分类，需删除该分类下的门店');
        }

        $cates = pdo_fetchall("SELECT * FROM " . tablename('mx_shop_mendian_cate') . " WHERE id in( $id ) ");

        foreach ($cates as $cate) {
            pdo_update('mx_shop_mendian_cate', array('deleted' => 1, 'status' => 0), array('id' => $cate['id']));
            plog('mendian.cate.delete', "删除门店分类 <br/>分类信息:  ID: {$cate['id']}");
        }
        show_json(1, array('url' => referer()));

    }


}
